﻿using System;
using System.Collections.Generic;
using System.Text;

namespace StudentList
{

    /// <summary>
    /// 进行学生相关信息处理
    /// </summary>
    public class StudentEnviroments
    {
        //学生列表
        private static IDictionary<string, Student> _students = new Dictionary<string, Student>();
        //学生控件列表
        private static IDictionary<string, UCStudent> _studentUCs = new Dictionary<string, UCStudent>();
        //学号元素模板，用此模板比对过滤器的第一个字符来确定是否查询学号
        private static List<string> templete = new List<string>() { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "j" };

        public static IDictionary<string, Student> Students
        {
            get { return _students; }
            set { _students = value; }
        }

        public static IDictionary<string, UCStudent> StudentUCs
        {
            get { return _studentUCs; }
            set { _studentUCs = value; }
        }

        public static void Add(Student s)
        {
            if(Students.ContainsKey(s.StudentNo))
            {
                Students[s.StudentNo] = s;
                RefreshUI(s);
            }
        }

        /// <summary>
        /// 刷新或增加一个学生控件
        /// </summary>
        /// <param name="student"></param>
        private static void RefreshUI(Student student)
        {
            if(StudentUCs.ContainsKey(student .StudentNo ))
            {
                StudentUCs[student .StudentNo].BindData(student );
            }
            else
            {
                UCStudent ucs = new UCStudent();
                ucs.BindData(student );
                StudentUCs[student.StudentNo] = ucs;
            }
        }

        /// <summary>
        /// 从加载的字符串数组中处理学号姓名。
        /// </summary>
        /// <param name="studentArray"></param>
        public static void LoadFromStringArray(string[] studentArray)
        {
            _students.Clear();
            foreach (string s in studentArray)
            {
                string[] ss = s.Split(',');
                if (ss.Length < 2)
                    continue;
                Student student = StudentFactory.GetInstance();
                student.StudentNo = ss[0].Trim();
                student.StudentName = ss[1].Trim();
                _students[student.StudentNo] = student;
                RefreshUI(student);

            }

            //清理学生列表中不存在，但是控件列表中存在的
            foreach (string sno in new List<string > (_studentUCs.Keys))
            {
                if (!_students.ContainsKey(sno))
                    _studentUCs.Remove(sno);
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<UCStudent> GetStudentUI()
        {
            return new List<UCStudent>(StudentUCs.Values);
        }
        /// <summary>
        /// 根据过滤器获取学生控件
        /// 
        /// 如果过滤器为空则返回所有学生信息
        /// 如果过滤器第一个字符为学号元素模板之一，则过滤学号
        /// 否则过滤姓名
        /// </summary>
        /// <param name="filter"></param>
        /// <returns>
        /// 
        /// </returns>
        public static List<UCStudent> GetStudentUI(string filter)
        {
            //检验参数是否空
            filter = filter.Trim();
            if (string.IsNullOrEmpty(filter))
                return GetStudentUI();
            //确定从学号还是姓名中搜索
            bool isNo = true;
            if (!templete.Contains(filter.Substring(0, 1)))//过滤器第一个字符是否落在学号元素模板中
                isNo = false;
            //开始遍历
            List < UCStudent > list = new List<UCStudent>();
            foreach (string sno in _students.Keys )
            {
                if (isNo)
                {
                    if (sno.Contains(filter))
                        list.Add(_studentUCs[sno]);
                }
                else
                {
                    if (_students[sno].StudentName.Contains(filter))
                        list.Add(_studentUCs[sno]);
                }
            }

            return list;
        }
    }
}
